package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.WxMenu;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0
 * @ClassName: ColumnJdbcDao.java
 * @Description: 专业管理
 * @Date 2016-4-22 上午11:45:46
 */
@Repository
public class WxMenuDaoImpl extends BaseDaoImpl<WxMenu> {
    Logger logger = Logger.getLogger(WxMenuDaoImpl.class);

    /**
     * 获取微信菜单数量
     * @param pid
     * @return
     */
    public int getMenuCount(int pid){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select IFNULL(count(1),0) from t_wx_menu where pid = ? and is_del = 0 ");
        arrList.add(pid);
        return Integer.valueOf(findCount(sb.toString(),arrList).toString());
    }

    /**
     * 获取列表
     * @param unit_id
     * @return
     */
    public List<WxMenu> wxMenuList(int unit_id){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_wx_menu where unit_id = ? and is_del = 0 order by sort asc");
        arrList.add(unit_id);
        return findList(sb.toString(),arrList);
    }


    /**
     * 获取最大排序
     * @param unit_id
     * @return
     */
    public int getMaxSort(int unit_id){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select IFNULL(max(sort),0) from t_wx_menu where unit_id = ? and is_del = 0 ");
        arrList.add(unit_id);
        return Integer.valueOf(findCount(sb.toString(),arrList).toString());
    }

    /**
     * 更新
     * @param wxMenu
     */
    public void updateWxMenu(WxMenu wxMenu){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_wx_menu set update_time = now() ");
        if(!StringUtil.isEmpty(wxMenu.getName())){
            sb.append(",name=? ");
            arrList.add(wxMenu.getName());
        }
        if(!StringUtil.isEmpty(wxMenu.getEkey())){
            sb.append(",ekey=? ");
            arrList.add(wxMenu.getEkey());
        }
        if(!StringUtil.isEmpty(wxMenu.getType())){
            if("0".equals(wxMenu.getType())){
                sb.append(",type='' ");
            }else{
                sb.append(",type=? ");
                arrList.add(wxMenu.getType());
            }
        }
        if(!StringUtil.isEmpty(wxMenu.getUrl())){
            sb.append(",url=? ");
            arrList.add(wxMenu.getUrl());
        }
        if(!StringUtil.isEmpty(wxMenu.getPagepath())){
            sb.append(",pagepath=? ");
            arrList.add(wxMenu.getPagepath());
        }
        sb.append("where id= ?");
        arrList.add(wxMenu.getId());
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delWxMenu(int id){
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_wx_menu set update_time = now(),is_del = 1 where id=? ");
        arrList.add(id);
        return updateJdbc(sb.toString(),arrList);
    }

}
